package org.example.projecttwo.service.Impl.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.projecttwo.entity.user.User;
import org.example.projecttwo.entity.user.UserTypeAuth;
import org.example.projecttwo.mapper.user.UserMapper;
import org.example.projecttwo.mapper.user.UserTypeAuthMapper;
import org.example.projecttwo.service.Iuser.IUserTypeAuthService;
import org.example.projecttwo.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户认证信息表 服务实现类
 * </p>
 *
 * @author nn
 * @since 2025-11-01
 */
@Service
public class UserTypeAuthServiceImpl extends ServiceImpl<UserTypeAuthMapper, UserTypeAuth> implements IUserTypeAuthService {

    @Autowired
    private BaseService baseService;
    
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取当前管理员ID
     * 验证当前用户是否为管理员角色
     * @return 管理员ID
     * @throws RuntimeException 当前用户不是管理员时抛出异常
     */
    private Long getCurrentAdminId(){
        Long currentUserId = baseService.getCurrentUserId();
        
        // 查询用户信息
        User currentUser = userMapper.selectById(currentUserId);
        if (currentUser == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 验证用户是否为管理员角色 (ADMIN 或 SUPER_ADMIN)
        String roleCode = currentUser.getRoleCode();
        if (!"ADMIN".equals(roleCode) && !"SUPER_ADMIN".equals(roleCode)) {
            throw new RuntimeException("只有管理员才能执行审核操作");
        }
        
        return currentUserId;
    }

    @Override
    public UserTypeAuth getCurrentUserAuth() {
        Long currentUserId = baseService.getCurrentUserId();

        return this.lambdaQuery()
                .eq(UserTypeAuth::getUserId, currentUserId)
                .last("LIMIT 1")
                .one();
    }

    @Override
    @Transactional
    public boolean submitUserAuth(UserTypeAuth userTypeAuth) {
        Long currentUserId = baseService.getCurrentUserId();
        userTypeAuth.setUserId(currentUserId);
        userTypeAuth.setAuthApplyTime(LocalDateTime.now());
        userTypeAuth.setAuthStatus(0); // 0-待审核
        return this.save(userTypeAuth);
    }

    @Override
    @Transactional
    public boolean auditUserAuth(Long authId, Integer status, String rejectReason) {
        UserTypeAuth userTypeAuth = this.getById(authId);
        if (userTypeAuth == null) {
            return false;
        }
        userTypeAuth.setAuthStatus(status);
        userTypeAuth.setAuditTime(LocalDateTime.now());
        userTypeAuth.setAuditAdminId(getCurrentAdminId()); // 获取审核管理员ID
        if (status == 2) { // 2表示拒绝
            userTypeAuth.setRejectReason(rejectReason);
        }
        
        // 如果审核通过，更新用户表中的认证类型
        if (status == 1) { // 1表示通过
            User user = new User();
            user.setId(userTypeAuth.getUserId());
            user.setAuthType(userTypeAuth.getAuthType());
            userMapper.updateById(user);
        }
        
        return this.updateById(userTypeAuth);
    }
}